package com.selfdriving.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;

import com.selfdriving.activity.RegisterTwoActivity;

public class SMSReceiver extends BroadcastReceiver
{
	public static final String TAG = "SMSReceiver"; 
	public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context context, Intent intent)
	{
		try
		{

			if (intent.getAction().equals(SMS_RECEIVED_ACTION))
			{
				SmsMessage[] messages = getMessagesFromIntent(intent);
				String str = messages[0].getDisplayMessageBody();
				str = str.substring(4, 9);
				System.out.println(str);
				if (RegisterTwoActivity.numbers != null)
				{
					RegisterTwoActivity.numbers.setText(str);
				}
				// System.out.println(str);
				// String regex = "\\d*";
				// Pattern p = Pattern.compile(regex);
				// Matcher m = p.matcher(str);
				// while (m.find())
				// {
				// if (!"".equals(m.group()))
				// {
				// String code = m.group();
				// System.out.println(code);
				// ForgetPass.EauthCode.setText(code);
				// }
				// }
			}
		} catch (Exception e)
		{
			// TODO: handle exception
		}
	}

	public final SmsMessage[] getMessagesFromIntent(Intent intent)
	{
		Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
		byte[][] pduObjs = new byte[messages.length][];
		for (int i = 0; i < messages.length; i++)
		{
			pduObjs[i] = (byte[]) messages[i];
		}
		byte[][] pdus = new byte[pduObjs.length][];
		int pduCount = pdus.length;
		SmsMessage[] msgs = new SmsMessage[pduCount];
		for (int i = 0; i < pduCount; i++)
		{
			pdus[i] = pduObjs[i];
			msgs[i] = SmsMessage.createFromPdu(pdus[i]);
		}
		return msgs;
	}

	// 将短信内容写到SD卡上的文件里，便于将文件pull到PC，这样可方便其它如WWW/WAP平台的自动化
	public void writeFile(String str)
	{
		String filePath = "/mnt/sdcard/verifyCode.txt";
		byte[] bytes = str.getBytes();
		try
		{
			File file = new File(filePath);
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(bytes);
			fos.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}